#include <string>
#include "MapAward.h"

MapAward::MapAward()
{
    mHitCount = 0;
    mHitCountStep = 0;

    mThroughMapRate = 0;
    mThroughMapRateStep = 0;

    mThroughMapScore = 0;
    mThroughMapScoreStep = 0;
    
    mThroughMapCount = 0;
    mThroughMapCountStep = 0;
}

void MapAward::unpackFromJson(const rapidjson::Value& json)
{
    mHitCount = json.FindMember("HitCount")->value.GetInt();
    mHitCountStep = json.FindMember("HitCountStep")->value.GetInt();
    mThroughMapRate = json.FindMember("MapRate")->value.GetInt();
    mThroughMapRateStep = json.FindMember("MapRateStep")->value.GetInt();
    mThroughMapScore = json.FindMember("MapScore")->value.GetInt();
    mThroughMapScoreStep = json.FindMember("MapScoreStep")->value.GetInt();
    mThroughMapCount = json.FindMember("MapCount")->value.GetInt();
    mThroughMapCountStep = json.FindMember("MapCountStep")->value.GetInt();
}

rapidjson::Value MapAward::packToJson(rapidjson::Document& doc)
{
    rapidjson::Value value(rapidjson::kObjectType);

    value.AddMember("HitCount", mHitCount, doc.GetAllocator());
    value.AddMember("HitCountStep", mHitCount, doc.GetAllocator());
    value.AddMember("MapRate", mHitCount, doc.GetAllocator());
    value.AddMember("MapRateStep", mHitCount, doc.GetAllocator());
    value.AddMember("MapScore", mHitCount, doc.GetAllocator());
    value.AddMember("MapScoreStep", mHitCount, doc.GetAllocator());
    value.AddMember("MapCount", mHitCount, doc.GetAllocator());
    value.AddMember("MapCountStep", mHitCount, doc.GetAllocator());

    return value;
}

int MapAward::getHitCount() const
{
    return mHitCount;
}

void MapAward::updateHitCount(int value)
{
    if (value > mHitCount)
    {
        mHitCount = value;
    }
}

int MapAward::getHitCountStep() const
{
    return mHitCountStep;
}

void MapAward::incHitCountStep()
{
    mHitCountStep += 1;
}

int MapAward::getThroughMapRate() const
{
    return mThroughMapRate;
}

void MapAward::updateThroughMapRate(int value)
{
    if (value > mThroughMapRate)
    {
        mThroughMapRate = value;
    }
}

int MapAward::getThroughMapRateStep() const
{
    return mThroughMapRateStep;
}

void MapAward::incThroughMapRateStep()
{
    mThroughMapRate += 1;
}

int MapAward::getThroughMapScore() const
{
    return mThroughMapScore;
}

void MapAward::updateThroughMapScore(int value)
{
    if (value > mThroughMapScore)
    {
        mThroughMapScore = value;
    }
}

int MapAward::getThroughMapScoreStep() const
{
    return mThroughMapScoreStep;
}

void MapAward::incThroughMapScoreStep()
{
    mThroughMapScoreStep += 1;
}

int MapAward::getThroughMapCount() const
{
    return mThroughMapCount;
}

void MapAward::incThroughMapCount(int incValue)
{
    mThroughMapCount += incValue;
}

int MapAward::getThroughMapCountStep() const
{
    return mThroughMapCountStep;
}

void MapAward::incThroughMapCountStep()
{
    mThroughMapCount += 1;
}

MapAward::PTR MapAwardMgr::findMapAwardByMapID(int mapID)
{
    auto it = mAllMapAward.find(mapID);
    if (it != mAllMapAward.end())
    {
        return (*it).second;
    }
    else
    {
        return nullptr;
    }
}

void MapAwardMgr::addMapAward(MapAward::PTR ma, int mapID)
{
    mAllMapAward[mapID] = ma;
}

void MapAwardMgr::unpackFromJson(const rapidjson::Value& json)
{
    if (!json.IsObject())
    {
        return;
    }

    for (rapidjson::Value::ConstMemberIterator it = json.MemberBegin(); it != json.MemberEnd(); ++it)
    {
        MapAward::PTR ma = std::make_shared<MapAward>();
        ma->unpackFromJson((*it).value);

        int id;
        sscanf((*it).name.GetString(), "%d", &id);
        mAllMapAward[id] = ma;
    }
}

rapidjson::Value MapAwardMgr::packToJson(rapidjson::Document& doc)
{
    rapidjson::Value value(rapidjson::kObjectType);

    for (auto& it : mAllMapAward)
    {
        auto& v = it.second;
        value.AddMember(rapidjson::Value(std::to_string(it.first).c_str(), doc.GetAllocator()), v->packToJson(doc), doc.GetAllocator());
    }

    return value;
}

MapAwardTemplate::HitCount::PTR MapAwardTemplate::getHitCountAward(int level)
{
    if (level >= 0 && level < mHitCountAward.size())
    {
        return mHitCountAward[level];
    }
    else
    {
        return nullptr;
    }
}

MapAwardTemplate::ThroughRate::PTR MapAwardTemplate::getThroughRateAward(int level)
{
    if (level >= 0 && level < mThroughRateAward.size())
    {
        return mThroughRateAward[level];
    }
    else
    {
        return nullptr;
    }
}

MapAwardTemplate::ThroughScore::PTR MapAwardTemplate::getThroughScoreAward(int level)
{
    if (level >= 0 && level < mHitThroughScoreAward.size())
    {
        return mHitThroughScoreAward[level];
    }
    else
    {
        return nullptr;
    }
}

MapAwardTemplate::ThroughCount::PTR MapAwardTemplate::getThroughCountAward(int level)
{
    if (level >= 0 && level < mThroughCountAward.size())
    {
        return mThroughCountAward[level];
    }
    else
    {
        return nullptr;
    }
}

void MapAwardTemplate::addHitCountAward(HitCount::PTR value, size_t level)
{
    if (mHitCountAward.size() < level)
    {
        mHitCountAward.resize(mHitCountAward.size() + level, nullptr);
    }

    mHitCountAward[level] = value;
}

void MapAwardTemplate::addThroughRateAward(ThroughRate::PTR value, size_t level)
{
    if (mThroughRateAward.size() < level)
    {
        mThroughRateAward.resize(mThroughRateAward.size() + level, nullptr);
    }

    mThroughRateAward[level] = value;
}

void MapAwardTemplate::addThroughScoreAward(ThroughScore::PTR value, size_t level)
{
    if (mHitThroughScoreAward.size() < level)
    {
        mHitThroughScoreAward.resize(mHitThroughScoreAward.size() + level, nullptr);
    }

    mHitThroughScoreAward[level] = value;
}

void MapAwardTemplate::addThroughCountAward(ThroughCount::PTR value, size_t level)
{
    if (mThroughCountAward.size() < level)
    {
        mThroughCountAward.resize(mThroughCountAward.size() + level, nullptr);
    }

    mThroughCountAward[level] = value;
}

void MapAwardTemplateMgr::load()
{
    MapAwardTemplate::PTR test = std::make_shared<MapAwardTemplate>();
    MapAwardTemplate::HitCount::PTR award = std::make_shared<MapAwardTemplate::HitCount>();
    test->addHitCountAward(award, 1);
}

MapAwardTemplate::PTR MapAwardTemplateMgr::findMapAwardTemplateByMapID(int mapID)
{
    auto it = mAllMapAward.find(mapID);
    if (it != mAllMapAward.end())
    {
        return (*it).second;
    }
    else
    {
        return nullptr;
    }
}